﻿using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Brain.Dev
{
    public class DefaultDebugManager : IDebugManager
    {
        public void DeserializedAll()
        {
        }
        public bool ShouldUpdate { get; set; }
        public bool ShouldDraw { get; set; }

        public List<GetString> DebugInfos;

        /// <summary>
        /// The Debug Manager has it's own spriteBatch
        /// </summary>
        public SpriteBatch SpriteBatch;

        public SpriteFont DefaultFont;

        public DefaultDebugManager()
        {
            DebugInfos = new List<GetString>();
            DebugInfos.Add(GetUpdateTime);
            DebugInfos.Add(GetDrawTime);

            SpriteBatch = new SpriteBatch(Engine.Instance.GraphicsDevice);

            DefaultFont = Engine.Instance.ContentManager.Load<SpriteFont>("DefaultFont");

            ShouldUpdate = true;
            ShouldDraw = true;
        }

        public void AddDebugString(GetString get)
        {
            DebugInfos.Add(get);
        }

        string GetUpdateTime()
        {
            return "Update: " + FloatUtil.Format(Engine.Instance.UpdateTime, 2);
        }
        string GetDrawTime()
        {
            return "Draw: " + FloatUtil.Format(Engine.Instance.DrawTime, 2);
        }

        public void Reset()
        {
            DebugInfos = null;
            DebugInfos = new List<GetString>();

            DebugInfos.Add(GetUpdateTime);
            DebugInfos.Add(GetDrawTime);

            SpriteBatch = new SpriteBatch(Engine.Instance.GraphicsDevice);

            DefaultFont = Engine.Instance.ContentManager.Load<SpriteFont>("DefaultFont");
        }

        public void Update()
        {
        }


        public void Draw()
        {
            if (Engine.Instance.DebugMode)
            {
                if (DebugInfos.Count > 0)
                {
                    SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
                    for (int i = 0; i < DebugInfos.Count; i++)
                    {
                        GetString s = DebugInfos[i];
                        string str = s();
                        SpriteBatch.DrawString(DefaultFont, str, new Vector2(50, 50 + (i * 30)) + new Vector2(2), Color.Black);
                        SpriteBatch.DrawString(DefaultFont, str, new Vector2(50, 50 + (i * 30)), Color.White);
                    }
                    SpriteBatch.End();
                }
            }
        }


        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }
    }
}
